package cn.ml.gaoJi.jiHe.set;



import cn.ml.gaoJi.jiHe.list.NewsTitle;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetDemo {
    public static void main(String[] args) {
        NewsTitle title1 = new NewsTitle(1,"1-北京晴天了1","admid");
        NewsTitle title2 = new NewsTitle(2,"2-北京晴天了2","admid");
        NewsTitle title3 = new NewsTitle(3,"3-北京晴天了3","admid");
        NewsTitle title4 = new NewsTitle(4,"4-北京晴天了4","admid");
        NewsTitle title5 = new NewsTitle(5,"5-北京晴天了5","admid");
        NewsTitle title6 = new NewsTitle(6,"6-北京晴天了5","admid");
        NewsTitle title7 = new NewsTitle(7,"7-北京晴天了5","admid");

        Set set = new HashSet();
        set.add(title1);
        set.add(title2);
        set.add(title3);
        set.add(title4);
        set.add(title5);
        System.out.println("新闻总记录数："+set.size());
        //遍历新闻标题
        //set是无序的所以输出的结果也是无序的
        System.out.println("11----增强型for循环---------------------");
        for (Object s:set){
            NewsTitle title = (NewsTitle) s;
            System.out.println(title.getId()+"--"+title.getTitle());
        }
        System.out.println("22----iterator迭代器---------------------");
        Iterator itor = set.iterator();//获得迭代器itor
        //通过迭代器迭代出集合元素
        while (itor.hasNext()){
            NewsTitle title = (NewsTitle) itor.next();
            System.out.println(title.getId()+"--"+title.getTitle());
        }


    }
}
